﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;
using System.Data;

namespace EmployeeCheckIn
{
    public partial class CreateAdmin : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            int i;
            if (!IsPostBack)
            {
                i = GetID();
                string idGen = string.Format("admin{0:000}", i);
                hdf_AdminID.Value = i.ToString();
                //txtUserName.Text = idGen;
                TXTempty();
            }           
        }
        private void TXTempty ()
        {
            txtPassWord.Text = "";
            txtConfirmPass.Text = "";
            txtLastname.Text = "";
            txtFirstName.Text = "";
            txtPhone.Text = "";            
        }
        private int GetID()
        {
            int id;
            string sqltext = @"SELECT ISNULL (MAX(ADMIN_ID),0)
                                from TBL_LOG_IN";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection("Server=202.44.52.248;Database=DB_Personal;User Id=personal;Password=1234;");
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            try
            {
                sqlcomm.Connection.Open();
                id = (int)sqlcomm.ExecuteScalar() + 1;
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
                throw;
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();
            return id;
        }
        public void  AddAdmin(Log_inEntity enLog)
        {

            string sqltext =  @"insert into TBL_LOG_IN 
                           ([ADMIN_ID]
                          ,[USER_NAME]
                          ,[USER_PASSWORD]
                          ,[ADMIN_NAME]
                          ,[ADMIN_LASTNAME]
                          ,[ADMIN_PHONE]
                          ,[ADMIN_TITLE]                          
                          ,[ADMIN_STATUS_ID]
                          ,[ADMIN_DATE_EDIT])                           
                           values (@admin_id,@user_name,@user_password,@admin_name,@admin_lastname,@admin_phone,@admin_title,@admin_status_id,GETDATE())";


            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection("Server=202.44.52.248;Database=DB_Personal;User Id=personal;Password=1234;");
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@admin_id", enLog.adminID);
            sqlcomm.Parameters.AddWithValue("@user_password", enLog.password);
            sqlcomm.Parameters.AddWithValue("@user_name", enLog.username);
            sqlcomm.Parameters.AddWithValue("@admin_name", enLog.adminNAME);
            sqlcomm.Parameters.AddWithValue("@admin_title", enLog.adminTITLE);
            sqlcomm.Parameters.AddWithValue("@admin_lastname", enLog.adminLASTNAME);
            sqlcomm.Parameters.AddWithValue("@admin_phone", enLog.adminPHONE);
            sqlcomm.Parameters.AddWithValue("@admin_status_id",enLog.adminSTATUS_ID);
           
           
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public bool checknumber(string s)
        {
            //bool result = false;
            //string Str = s.Trim();
            //double Num;
            //bool isNum = double.TryParse(Str, out Num);
            //if (isNum)
            //{ 
            //    result = true; 
            //}
            //return result;

            bool result = false;
            try
            {
                Convert.ToDouble(s);
                result = true;
            }
            catch (Exception)
            {
                result = false;

            }
            return result;
        }
        public DataTable GetUserNameInDb(string U)
        {

            DataTable dt = new DataTable();
            string sqltext = "select * from TBL_LOG_IN Where user_name like '" + U + "'; ";
            SqlConnection sqlconn = new SqlConnection("Server=202.44.52.248;Database=DB_Personal;User Id=personal;Password=1234;");
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];  
        }
        private void lbEmpty()
        {
            lbConfirmPass.Visible = false;
            lbConPhone.Visible = false;
            lbWarnAddmin.Visible = false;
            lbWarnName.Visible = false;
            lbWarnNumPass.Visible = false;
            lbWarnUser.Visible = false;
        }
        protected void btCreatAddminOK_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            lbEmpty();
            if (Session["User"] != null && Session["Pass"] != null)
            {
                Log_inEntity Enlog = new Log_inEntity();
                bool resualt = false;
                string Username = txtUserName.Text.ToLower();
                DataTable dt = GetUserNameInDb(Username);
                if (txtUserName.Text == "")
                {
                    lbWarnAddmin.Visible = true;
                    lbConfirmPass.Visible = false;
                    lbWarnNumPass.Visible = false;
                    resualt = true;
                }
                if (dt != null && dt.Rows.Count > 0)
                {
                    //string UserDB = dt.Rows[0]["user_name"].ToString();
                    lbWarnUser.Visible = true;
                    resualt = true;
                    //username avalible            
                }                 
                if (txtPassWord.Text != txtConfirmPass.Text)
                {
                    lbConfirmPass.Visible = true;
                    lbWarnAddmin.Visible = false;
                    lbWarnNumPass.Visible = false;
                    resualt = true;
                }
                if (txtFirstName.Text== "")
                {
                    
                    lbWarnName.Visible = true;
                    resualt = true;
                }
                if (txtLastname.Text == "")
                {
                    lbWarnlsn.Visible = true;
                    resualt = true;
                }
                if (checknumber(txtPhone.Text) == false)
                {
                    if (txtPhone.Text == "")
                    {
                        resualt = false;
                    }
                    else
                    {
                        lbConPhone.Visible = true;
                        resualt = true;
                    }
                   
                }
                if (txtUserName.Text != "" && (txtPassWord.Text == txtConfirmPass.Text))
                {
                    
                    if (txtFirstName.Text=="")
                    {
                        resualt = true;
                        lbWarnName.Visible = true;
                    }
                    if (txtPassWord.Text.Length < 8)
                    {
                        lbWarnNumPass.Visible = true;
                        resualt = true;
                    }
                   
                }
                if (resualt == true)
                {
                    return;
                }
                if (resualt == false)
                {   
                    Enlog.adminID = Convert.ToInt32(hdf_AdminID.Value);
                    Enlog.username = txtUserName.Text.ToLower();
                    Enlog.adminLASTNAME = txtLastname.Text;
                    Enlog.adminPHONE = txtPhone.Text;
                    Enlog.adminNAME = txtFirstName.Text;
                    Enlog.adminSTATUS_ID = Convert.ToInt32(ddlAdminStatus.SelectedValue);
                    Enlog.adminTITLE = Convert.ToInt32(rblTitleAdmin.SelectedValue);
                    Enlog.password = EncyptMd5.getMd5Hash(txtPassWord.Text);
                    AddAdmin(Enlog);
                    lbConfirmPass.Visible = false;
                    lbWarnAddmin.Visible = false;
                    lbWarnNumPass.Visible = false;
                    Response.Write("<script>alert('สร้างผู้ดูแลเรียบร้อยแล้ว!'); window.location='ListAdmin.aspx';</script>");


                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null && Session["Pass"] != null)
            {
                Server.Transfer("ListAdmin.aspx");
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("CreateAdmin.aspx"); 
        }

       
    }
}